package com.tagtraum.perf.gcviewer.imp;

/*
 * autor:wenguang.huang 2014.3.1 
 */
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import com.tagtraum.perf.gcviewer.model.GCModel;

public class DataReaderIBM_J9_2_6 implements DataReader {

	
	
    private static Logger LOG = Logger.getLogger(DataReaderIBM_J9_2_6.class.getName());
    
    private InputStream in;
    
     public DataReaderIBM_J9_2_6(final InputStream in) {
    	 this.in = in;
    }
	@Override
	public GCModel read() throws IOException {
		// TODO Auto-generated method stub
		
		
		  if (LOG.isLoggable(Level.INFO)) LOG.info("Reading IBM J9 2.6 format...");
	        try {
	            final GCModel model = new GCModel(true);
	            model.setFormat(GCModel.Format.IBM_VERBOSE_GC_2_6);           
	            DefaultHandler handler = new IBMJ92_6Handler(model);

	           //added by wenguang.huang 
	      
	            
	            byte[] buffer = new byte[2048];
	            int readBytes = 0;
	            int length =0;
	            StringBuilder stringBuilder = new StringBuilder();
	            in.mark(Integer.MAX_VALUE);
	            while((readBytes = in.read(buffer)) > 0){
	                stringBuilder.append(new String(buffer, 0, readBytes));
	                length =length + readBytes;
	            }
	            in.reset();
	            model.setGccontent(stringBuilder.toString());
				//added end by wenguang.huang 

	            //in.read(buffer);
	            // Use the default (non-validating) parser
	            SAXParserFactory factory = SAXParserFactory.newInstance();
	            factory.setValidating(false);
	            
	            javax.xml.parsers.SAXParser saxParser;
				try {
					saxParser = factory.newSAXParser();
					saxParser.parse( in, handler );
					

				} catch (ParserConfigurationException e) {
	                final IOException exception = new IOException(e.toString());
	                exception.initCause(e);
	                throw exception;
				} catch (SAXException e) {
					// TODO: if(e.getMessage().startsWith("XML document structures must start and end within the same entity")) {
					if (e instanceof SAXParseException && ((SAXParseException) e).getColumnNumber() == 1) {
						// ignore. this just means a xml tag terminated.
					} else {
	                    final IOException exception = new IOException(e.toString());
	                    exception.initCause(e);
	                    throw exception;
					}
				}
	            
	            return model;
	  
	        } finally {
	            if (in != null)
	                try {
	                	
	                	
	                    in.close();
	                } catch (IOException ioe) {
	                }
	            if (LOG.isLoggable(Level.INFO)) LOG.info("Done reading.");
	        }
	}

}
